import java.awt.Image;
import java.awt.geom.Rectangle2D;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.swing.ImageIcon;

import com.sun.pdfview.PDFFile;

import model.rentals.NoSuchPageException;
import model.rentals.Page;
import services.viewer.Viewer;

/**
 * Classe SwingTextViewer para manipular documentos de texto
 * @author DCO37
 * 	N.º 42484 Helder Ferreira
 *  N.º 45509 Luis Ferreira
 */
public class SwingTextViewer extends Viewer {
	
//	private PDFFile pdffile;
	
	private File docFile;
	
	/**
	 * Construtor SwingTextViewer
	 */
	public SwingTextViewer() {
		super("swing");
	}

	@Override
	/*
	 * (non-Javadoc)
	 * @see services.viewer.Viewer#loadMimeTypes()
	 */
	protected void loadMimeTypes() {
		List<String> mimeTypes = new ArrayList<String>(1);
		mimeTypes.add("plain/text");
		setMimeTypes (mimeTypes);
	}
	
	@Override
	public void setDocument (File file) throws IOException {
		docFile = file;
		super.setDocument(docFile);
	}

	@Override
	public Object getPage(int pageNum, int width, int height) throws NoSuchPageException {
		if (pageNum < 1)
			throw new NoSuchPageException();
		Page novaPagina = new Page(pageNum);
		try {
			Scanner in = new Scanner(new FileReader(docFile));
			int linhaActual = 1;
			while(in.hasNext() && linhaActual < height){
				novaPagina.addAnnotation(in.toString());
				in.next();
				linhaActual++;	
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	    return novaPagina;
	}

	@Override
	/*
	 * (non-Javadoc)
	 * @see services.viewer.Viewer#canSlideshow()
	 */
	public boolean canSlideshow() {
		return true;
	}

}
